package com.quanxi.qxaimain.controller.v1.demo;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.quanxi.qxaimain.common.util.execlExporttool.SXSSFWorkbookExporter;

import com.quanxi.qxaimain.service.demoserver;
import com.quanxi.qxaimain.service.hystrixServer.hystrixServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static java.lang.Thread.sleep;

/**
 * @Author lyx
 * @create 2022/9/30 16:10
 */
@Slf4j
@RestController
@RequestMapping("/demo")
public class demoController {

    @Resource
    demoserver demoserver;


    @PostMapping(value = "/test")
    public  Object tt(@RequestBody JSONObject params) throws IOException, InterruptedException, ExecutionException {
        List<String> list= JSONObject.parseArray(params.getJSONArray("list").toString(),String.class);

        demoserver.springAsynchronousMethod();

        return list;
    }

    @RequestMapping(value = "/excel/downloadExcel", method= RequestMethod.GET )
    public void downloadExcel(HttpServletResponse response) throws Exception{
        String fileName = "人员基础信息.xlsx";
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
     //   response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
        response.setHeader("content-disposition","attachment;filename="+new String(fileName.getBytes("gb2312"), "ISO8859-1"));
        OutputStream outputStream = response.getOutputStream();
         downloadExcelTemplate(fileName, outputStream);


    }


    @RequestMapping(value = "/getchild", method= RequestMethod.POST )
    public Object getchild(@RequestBody Object json) throws Exception{

        List<JSONObject> childs=new ArrayList<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","张四");
        jsonObject.put("sex","男");
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("name","张五");
        jsonObject2.put("sex","女");

        childs.add(jsonObject);
        childs.add(jsonObject2);



        return  childs;

    }






  private void downloadExcelTemplate ( String fileName , OutputStream outputStream) throws Exception{

      Thread.sleep(10000);
      List<Map<String, Object>> datas = new ArrayList();
      for (int i = 0; i < 2066; i++) {
          Map data = new HashMap();
          data.put("name", "name" + i);
          data.put("age", "age" + i);
          datas.add(data);
      }

      if(datas.isEmpty() ||datas==null){
          Map<String, Object> map =new HashMap<String, Object>();
          map.put(fileName,"无数据，请检查搜索条件");
          datas.add(map);
      }

      Map<String, Object> map = datas.get(0);
      Set<String> set = map.keySet();
      List<String> list = new ArrayList(set);
      String[] headname = new String[list.size()];
      list.toArray(headname ); //获取Excel列名


      /**
       * 将List<Map<String, Object>>写出到excel中
       */
      SXSSFWorkbookExporter hssfWorkExcel = new SXSSFWorkbookExporter( headname, fileName.replace(".xlsx",""));//初始化execl
      hssfWorkExcel.createTableRows(datas, headname);//插入数据
      try {
       //   hssfWorkExcel.exportExcel(new FileOutputStream(new File("F:\\360MoveData\\Users\\lyx\\Desktop\\bigXmlConvertDB-master\\人员基本信息.xlsx"))); //导出数据
           hssfWorkExcel.exportExcel(outputStream);
      } catch (Exception e) {
          e.printStackTrace();
      }

  }



}